package cn.walls1717.juc.cas;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * AtoRefStamp测试
 *
 * @author walls1717
 * @create 2022/12/12 14:07
 **/
@Slf4j
public class AtomicStampedDemo {

    public static void main(String[] args) {
        Book book = new Book();
        book.setId(1);
        book.setName("Java");

        AtomicStampedReference<Book> ato = new AtomicStampedReference<>(book, 1);
        log.info(ato.getReference() + "  " + ato.getStamp());

        Book book1 = new Book();
        book1.setId(2);
        book1.setName("MySQL");

        boolean flag = ato.compareAndSet(book, book1, ato.getStamp(), ato.getStamp() + 1);
        log.info(flag + "  " + ato.getReference() + "  " + ato.getStamp());

        boolean flag2 = ato.compareAndSet(book1, book, ato.getStamp(), ato.getStamp() + 1);
        log.info(flag2 + "  " + ato.getReference() + "  " + ato.getStamp());

    }
}

@Data
class Book {
    private Integer id;
    private String name;
}
